package asia.huangxi.project.service;

import asia.huangxi.project.model.GradleOrder;
import asia.huangxi.project.repository.OrderRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class OrderService {

    @Resource
    private OrderRepository orderRepository;

    public Long save(GradleOrder order) {
        GradleOrder save = orderRepository.save(order);
        return save.getId();
    }

    public String get(Long orderId) {
        Optional<GradleOrder> order = orderRepository.findById(orderId);
        if(!order.isPresent()){
            return "error";
        }
        log.info(order.toString());
        return "订单ID:" + order.get().getId();
    }

    public List<GradleOrder> list() {
        return orderRepository.findAll();
    }


    /**
     * 将所有的订单发货
     * @return
     */
    public Boolean shipAll() {
        List<GradleOrder> list = orderRepository.findAll();
        list.stream().forEach(l->l.setStatus(1));
        List<GradleOrder> all = orderRepository.saveAll(list);
        return all.size() == list.size();
    }

}